package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/driveschool")
public class GetDriveSchoolInfo extends BaseController {
	@Autowired
	private BizService bizService;
	private static Logger logger = LoggerFactory.getLogger(GetDriveSchoolInfo.class);
	@Autowired
	private BizDao bizDao;

	/**
	 * 驾校列表查询
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/GetDriveSchool", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String GetDriveSchool(HttpServletRequest request) {
		
		RestResponse response = new RestResponse();
		
		try {
			String PageNum = request.getParameter("PageNum");
			String PageSize = request.getParameter("PageSize");
			String regionNo = request.getParameter("regionNo");
			if ((PageNum != null && !PageNum.isEmpty()) || (PageSize != null
					&& !PageSize.isEmpty())) {
				if (PageNum.equals("0") || PageSize.equals("0")) {
					response.setErrorcode(1);
					response.setMessage("页码或页数不能为0...");
					return response.toJSON();
				}
			}
			if(regionNo != null && !regionNo.isEmpty() ){
				//获取请求体中的参数（除了这些不为空的确定参数，还有可能有其他参数）
				Map<String, Object> paramsMap = getParamMap(request);
				paramsMap.put("imgUrl", Config.getImageAddress());
				response = bizService.getDriveSchoolInfo(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("区县编号不能为空...");
				return response.toJSON();
			}
		} catch (Exception e) {
			errorHandler(e, response, logger);
		} finally {
			return response.toJSON();
		}
	}

	/**
	 * 驾校详情查询(驾校ID)
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/GetDriveSchoolDetailInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String GetDriveSchoolDetailInfo(
			HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 驾校id
			String driverId = request.getParameter("driverId");
			//驾校类型(1总校2分校)
			String driveType = request.getParameter("driveType");
			
			if ((driverId != null && !driverId.isEmpty())
					&& (driveType != null && !driveType.isEmpty())) {
				// 获取数据集合
				String imgUrl=Config.getImageAddress();
				response = bizService.getDriverSchoolDtail(driverId,driveType,imgUrl,response);	
			} else {
				response.setErrorcode(1);
				response.setMessage("必填参数不能为空...");
			}
		} catch (Exception e) {
			logger.info("GetDriveSchoolDetailInfo:" + e.toString());
			response.setReturnMsg(1, "获取驾校信息失败！");
		} finally {
			return response.toJSON();
		}

	}

	/**
	 * 驾校详情查询(按名称模糊查询)
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/GetDriveListInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String GetDriveListInfo(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			// 驾校名称
			String drivename = request.getParameter("drivename");
			if (drivename != null && !drivename.isEmpty()) {
				paramsMap.put("drivename", drivename);
				paramsMap.put("imgUrl", Config.getImageAddress());
				// 获取数据集合
				List<Map<String, Object>> map = bizDao.getDrivesInfo(paramsMap);

				if (map != null && map.size() > 0) {

					// 设置Json集合
					response.setData(map);
				} else {
					response.setErrorcode(1);
					response.setMessage("无驾校信息...");
				}
			} else {
				response.setErrorcode(1);
				response.setMessage("驾校名称不能为空...");
			}

		} catch (Exception e) {
			logger.info("getDriveSchoolListInfo:" + e.toString());
			response.setReturnMsg(1, "根据名称获取驾校信息失败！");
		} finally {
			return response.toJSON();
		}

	}
}
